'''

Case Type： 数据库系统
Case Name： 设置allow_system_table_mods参数的值为on
Create At:  2020/07/20
Owner    :  @li-xin12345
Descption:  测试非管理员修改allow_system_table_mods参数的值为on后，更改系统表失败
            1.设置allow_system_table_mods参数的值为on 2.重启数据库使参数生效 3.查找系统表pg_index 4.更改系统表结构
history：
created： 2020/07/20  created by @li-xin12345
'''

import os
import unittest
from yat.test import Node
from testcase.utils.CommonSH import *
import sys
import time
from yat.test import macro
from testcase.utils.CommonSH import *

from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger

logger = Logger()
sys.path.append(sys.path[0] + "/../")


class IndexFileDamaged(unittest.TestCase):
    sh_primy = CommonSH('PrimaryDbUser')
    sh_stadb = CommonSH('Standby1DbUser')
    nodes_tuple = ('PrimaryDbUser', 'Standby1DbUser', 'Standby2DbUser')

    @RestartDbCluster(*nodes_tuple)
    def setUp(self):
        logger.info('------------Opengauss_Reliability_Dbsys_Case210.py start------------')
        self.DB_ENV_PATH = macro.DB_ENV_PATH
        self.primaryNode = Node(node='PrimaryDbUser')
        self.standbyNode = Node(node='Standby1DbUser')
        self.Constant = Constant()

    def test_Index_file_damaged(self):
        # 配置参数
        logger.info('----------------------------设置参数allow_system_table_mods的值为on-----------------------------')
        mod_msg = self.sh_primy.executeGsguc('set', self.Constant.GSGUC_SUCCESS_MSG, 'allow_system_table_mods=on')
        self.assertTrue(mod_msg)

        # 重启使参数生效
        logger.info('----------------------------重启数据库使参数生效-----------------------------')
        excute_cmd = f'''source {self.DB_ENV_PATH};
                            gs_om -t stop;
                            gs_om -t start;
                            '''
        logger.info(excute_cmd)
        msg = self.primaryNode.sh(excute_cmd).result()
        logger.info(msg)
        self.assertIn(self.Constant.START_SUCCESS_MSG, msg)

        # 查找系统表pg_index
        logger.info('----------------------------查找系统表-----------------------------')
        sql_cmd = "select distinct(tablename) FROM pg_tables where tablename = 'pg_index';"
        excute_cmd = f'''
                            source {self.DB_ENV_PATH};
                            gsql -d {self.primaryNode.db_name} -p {self.primaryNode.db_port} -c "{sql_cmd}" '''
        logger.info(excute_cmd)
        msg = self.primaryNode.sh(excute_cmd).result()
        logger.info(msg)
        self.assertNotIn(self.Constant.NO_FILE_MSG, msg)

        # 更改系统表结构
        logger.info('----------------------------更改系统表pg_index表结构-----------------------------')
        sql_cmd = 'alter table pg_index modify (indexrelid smallint);'
        excute_cmd = f'''
                            source {self.DB_ENV_PATH};
                            gsql -d {self.primaryNode.db_name} -p {self.primaryNode.db_port} -c "{sql_cmd}";
                            gs_om -t status
                            '''
        logger.info(excute_cmd)
        # 更改失败，报错信息只在日志中有，但是数据库变为stop状态
        msg = self.primaryNode.sh(excute_cmd).result()
        self.assertIn(self.Constant.SQL_WRONG_MSG[1], msg)
        logger.info(msg)
        time.sleep(10)
        logger.info(10)

        # 重启数据库
        logger.info('----------------------------重启数据库-----------------------------')
        excute_cmd = f'''
                                    source {self.DB_ENV_PATH};
                                    gs_om -t stop;
                                    gs_om -t start;
                                    '''
        logger.info(excute_cmd)
        msg = self.primaryNode.sh(excute_cmd).result()
        logger.info(msg)
        self.assertIn(self.Constant.START_SUCCESS_MSG, msg)

        # 查看系统表pg_index表结构是否更改
        logger.info('----------------------------查找系统表结构-----------------------------')
        sql_cmd = '\d pg_index;'
        excute_cmd = f'''
                                    source {self.DB_ENV_PATH};
                                    gsql -d {self.primaryNode.db_name} -p {self.primaryNode.db_port} -c "{sql_cmd}" '''
        logger.info(sql_cmd)
        logger.info(excute_cmd)
        msg = self.primaryNode.sh(excute_cmd).result()
        logger.info(msg)
        self.assertNotIn(self.Constant.NO_FILE_MSG, msg)

        # 恢复参数值
        logger.info('----------------------------恢复参数值-----------------------------')
        mod_msg = self.sh_primy.executeGsguc('set', self.Constant.GSGUC_SUCCESS_MSG, 'allow_system_table_mods=off');
        self.assertTrue(mod_msg)

        # 重启使参数生效
        logger.info('----------------------------重启数据库使参数生效-----------------------------')
        excute_cmd = f'''
                                source {self.DB_ENV_PATH};
                                gs_om -t stop;
                                gs_om -t start;
                                '''
        logger.info(excute_cmd)
        msg = self.primaryNode.sh(excute_cmd).result()
        logger.info(msg)
        self.assertIn(self.Constant.START_SUCCESS_MSG, msg)

    def tearDown(self):
        logger.info('----------------------------后置处理-----------------------------')
        try:
            db_status = self.sh_primy.getDbInstanceStatus()
            logger.info(db_status)
            if db_status:
                pass
            else:
                is_started = self.sh_primy.startDbCluster()
                self.assertTrue(is_started)
                logger.info(f'{is_started}')
                logger.info(f'db_status: {is_started}')
        finally:
            mod_msg = self.sh_primy.executeGsguc('set', self.Constant.GSGUC_SUCCESS_MSG, 'allow_system_table_mods=off')
            self.assertTrue(mod_msg)
        logger.info('-----------------------Reliability_Dbsys_Case210执行完成-----------------------------')
